<?php



namespace Model;



use \Gas\Core;

use \Gas\ORM;



class User extends ORM {



	const STATUS_INACTIVE = 0;

	const STATUS_ACTIVE = 1;



	public $table = 'users';



	public $primary_key = 'id';



	public $foreign_key = array('\\model\\category' => 'cat_id');



	function _init()

	{



		self::$relationships = array (

			'promotions' => ORM::has_many('\\Model\\Promotion', array('order_by:date_from[desc]')),

			'favorites' => ORM::has_many('\\Model\\Favorite\\Promotion => \\Model\\Promotion'),

			'category' =>     ORM::belongs_to('\\Model\\Category'),

			'groups' => ORM::has_many('\\Model\\User_Group => \\Model\\Group')

		);



		self::$fields = array(

			'id' => ORM::field('auto[10]'),

			'username' => ORM::field('char[64]'),

			'password' => ORM::field('char[255]'),

			'email' => ORM::field('char[255]'),

		);

	}



	public function url()

	{

		$title = implode(' ', array($this->first_name, $this->last_name));

		return 'store/'.$this->id.'-'.url_title($title, '-', true);

	}



	public function fullname()

	{

		return $this->first_name . ' ' . $this->last_name;

	}



	// --------------------------------------------------------------------

	// Post Model Initialisation

	//   Add your own custom initialisation code to the Model

	// The parameter indicates if the current config was loaded from cache or not

	// --------------------------------------------------------------------

	public function group_name()

	{

		$group_names = array();



		foreach(ci()->auth_model->get_users_groups($this->id)->result() as $group_name)

		{

			$group_names[] = ucwords($group_name->name);

		}



		return implode(', ', $group_names);

	}



	// --------------------------------------------------------------------

	// Post Model Initialisation

	//   Add your own custom initialisation code to the Model

	// The parameter indicates if the current config was loaded from cache or not

	// --------------------------------------------------------------------

	public function group_ids()

	{

		$group_ids = array();



		foreach(ci()->auth_model->get_users_groups($this->id)->result() as $group)

		{

			$group_ids[] = ucwords($group->id);

		}



		return $group_ids;

	}



	// --------------------------------------------------------------------

	// Post Model Initialisation

	//   Add your own custom initialisation code to the Model

	// The parameter indicates if the current config was loaded from cache or not

	// --------------------------------------------------------------------

	public function register_date($format = 'Y-m-d h:i A')

	{

		return date($format, $this->created_on);

	}



	// --------------------------------------------------------------------

	// Post Model Initialisation

	//   Add your own custom initialisation code to the Model

	// The parameter indicates if the current config was loaded from cache or not

	// --------------------------------------------------------------------

	public function block()

	{

		return ci()->auth_model->deactive($this->id);

	}



	// --------------------------------------------------------------------

	// Post Model Initialisation

	//   Add your own custom initialisation code to the Model

	// The parameter indicates if the current config was loaded from cache or not

	// --------------------------------------------------------------------

	public function current_promotions()

	{

		return Promotion::where('created_by = '.$this->id.' AND date_from <= \''.date('Y-m-d H:i:s').'\' AND date_to >= \''.date('Y-m-d H:i:s').'\'')

				->order_by('date_from', 'ASC')

				->all();

	}



	// --------------------------------------------------------------------

	// Post Model Initialisation

	//   Add your own custom initialisation code to the Model

	// The parameter indicates if the current config was loaded from cache or not

	// --------------------------------------------------------------------

	public function past_promotions()

	{

		return Promotion::where('created_by = '.$this->id.' AND date_to < \''.date('Y-m-d H:i:s').'\'')

				->order_by('date_to', 'DESC')

				->all();

	}



	// --------------------------------------------------------------------

	// Post Model Initialisation

	//   Add your own custom initialisation code to the Model

	// The parameter indicates if the current config was loaded from cache or not

	// --------------------------------------------------------------------

	public function avatar($width = 150, $height = null)

	{

		return $this->avatar != ''

				? img(array(

					'src' => base_url('public/thumb.php?src='.base_url($this->avatar).'&w='.$width.(is_null($height) ? '' : ('&h='.$height))),

					'width' => $width,

					'height' => $height,

					'alt' => $this->fullname(),

					'class' => 'avatar'

				))

				: img(array(

					'src' => 'public/img/avatar.png',

					'width' => $width,

					'height' => $height,

					'alt' => $this->fullname(),

					'class' => 'avatar'

				));

	}



	// --------------------------------------------------------------------

	// Post Model Initialisation

	//   Add your own custom initialisation code to the Model

	// The parameter indicates if the current config was loaded from cache or not

	// --------------------------------------------------------------------

	public function has_avatar()

	{

		return ($this->avatar != '');

	}



	// --------------------------------------------------------------------

	// Post Model Initialisation

	//   Add your own custom initialisation code to the Model

	// The parameter indicates if the current config was loaded from cache or not

	// --------------------------------------------------------------------

	public function has_map()

	{

		return ($this->longitude != '' and $this->latitude != '');

	}



	// --------------------------------------------------------------------

	// Post Model Initialisation

	//   Add your own custom initialisation code to the Model

	// The parameter indicates if the current config was loaded from cache or not

	// --------------------------------------------------------------------

	public function profile($field_name = '')

	{

		$profile_fields = @unserialize($this->profile_fields);



		if($profile_fields)

		{

			foreach($profile_fields as $field => $value)

			{

				if($field == $field_name)

				{

					return $value;

				}

			}

		}

	}



	public function status()

	{

		if($this->id == 1) return '';

		switch($this->active)

		{

			case 0:

				return 'Blocked'; break;

			case 1:

				return 'Active'; break;

			default:

				return ''; break;

		}

	}



}